﻿using System;
using System.ComponentModel;
using Brjnk.MVVM;

namespace Brjnk.GpxExplorer.Data
{
    public abstract class GeocacheInfo : System.ComponentModel.INotifyPropertyChanged
    {
        //  public abstract string ID { get; set; }

        public abstract string Name { get; set; }

        public abstract float Terrain { get; set; }

        public abstract float Difficulty { get; set; }

        public abstract GeocacheSize? Size { get; set; }

        public abstract GeocacheType? Type { get; set; }

        public abstract string Author { get; set; }

        public abstract string Hint { get; set; }

        public abstract string HtmlShort { get; set; }

        private ObservableCollectionEx<GeocacheLog> logs;

        public ObservableCollectionEx<GeocacheLog> Logs
        {
            get { return logs; }
            set { logs = value; OnPropertyChanged("Logs"); }
        }

        public abstract string HtmlBody { get; set; }

        public T ConvertTo<T>() where T : GeocacheInfo
        {
            var instance = Activator.CreateInstance(typeof(T)) as GeocacheInfo;
            CloneLogs(instance);
            instance.Name = Name;
            instance.Terrain = Terrain;
            instance.Difficulty = Difficulty;
            instance.Size = Size;
            instance.Type = Type;
            instance.Author = Author;
            instance.Hint = Hint;
            instance.HtmlShort = HtmlShort;
            instance.HtmlBody = HtmlBody;
            return instance as T;
        }

        private void CloneLogs(GeocacheInfo instance)
        {
            instance.Logs = new ObservableCollectionEx<GeocacheLog>();
            if (this.Logs != null) 
            foreach (var log in Logs)
            {
                instance.Logs.Add((GeocacheLog)log.Clone());
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}